package com.sservice.commonBusiness.controller.web;

import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.Comment;
import com.sservice.commonBusiness.e.DataR;
import com.sservice.commonBusiness.mapper.CommentMapper;
import com.sservice.commonBusiness.service.CommentService;
import com.sservice.commonBusiness.service.DataRService;
import com.sservice.commonBusiness.service.StatisticService;
import com.sservice.commonBusiness.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/comment1/")
public class Comment1Controller {

    @Autowired
    CommentService commentService;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    StatisticService statisticService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(Comment comment) {
        comment.setUserId(comment.getUserId());// 评论用户通过前端传值
        CommentVo commentVo = new CommentVo();
        commentVo.setUserId(comment.getUserId());
        commentVo.setTargetId(comment.getTargetId());
        commentVo.setStatus(1);
        int total = commentMapper.pageCount(commentVo);
        if (total > 0) {
            return new BaseResult(false, "您已评论，不能再次评论！", 403);
        }
        return new BaseResult(true, String.valueOf(commentService.insertSelective(comment)), 200);
    }

    /**
     * 评论，不限次数
     * @param comment
     * @return
     */
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult comment(Comment comment) {
        return new BaseResult(true, String.valueOf(commentService.insertSelective(comment)), 200);
    }

    @Autowired
    DataRService dataRService;

    /**
     * 评论点赞统计
     * @param dataR
     * @return
     */
    @RequestMapping(value = "statisticByDr", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult statistic(DataR dataR) {
        Comment comment = new Comment();
        comment.setId(dataR.getSourceId());
        comment.setHot(dataRService.selectBy(dataR).size());
        commentMapper.updateByPrimaryKeySelective(comment);
        return new BaseResult(true, "",200);
    }
    @RequestMapping(value = "statisticScoreByDr", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult statisticScoreByDr(DataR dataR) {
        Comment comment = new Comment();
        comment.setId(dataR.getSourceId());
        comment.setHot(dataRService.selectBy(dataR).size());
        commentMapper.updateByPrimaryKeySelective(comment);
        return new BaseResult(true, "",200);
    }

    /**
     * 某个问题的回复统计
     *
     * @param commentVo
     * @return
     */
    @RequestMapping(value = "count/{targetId}")
    @ResponseBody
    public BaseResult count(CommentVo commentVo) {
        return new BaseResult(true, (commentService.pageCount(commentVo)));
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(@RequestParam Integer id) {
        return new BaseResult(true, String.valueOf(commentService.deleteByPrimaryKey(id)), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(Comment comment) {
        return new BaseResult(true, String.valueOf(commentService.updateByPrimaryKeySelective(comment)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(@RequestParam Integer id) {
        Comment comment = commentService.selectByPrimaryKey(id);
        return new BaseResult(true, comment);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(CommentVo commentVo) {
        List<CommentVo> list = commentMapper.page(commentVo);
        Pagination pagination = new Pagination();
        pagination.setTotal(commentMapper.pageCount(commentVo));
        ListResult lr = new ListResult(true, list);
        lr.setPage(pagination);
        return lr;
    }
}
